package com.rc.evcharger.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Create by Yang on 2021/5/6
 */
public class CP56time2aUtil {

    /**
     * 获取日期数组
     *
     * @return
     */
    public static byte[] getTimeBytesCP56time2a(String tim) {
        int yy = Integer.valueOf(tim.substring(2, 4));
        int MO = Integer.valueOf(tim.substring(4, 6));
        int DA = Integer.valueOf(tim.substring(6, 8));
        int HH = Integer.valueOf(tim.substring(8, 10));
        int MM = Integer.valueOf(tim.substring(10, 12));
        byte[] SS = int2Bytes(Integer.valueOf(tim.substring(12)) * 1000);

        byte[] timedata = new byte[]
                {SS[0], SS[1], (byte) MM, (byte) HH, (byte) DA, (byte) MO, (byte) yy,};
        return timedata;
    }

    public static byte[] int2Bytes(int num) {
        byte[] bytes = new byte[4];
        //通过移位运算，截取低8位的方式，将int保存到byte数组
        bytes[0] = (byte) (num >>> 24);
        bytes[1] = (byte) (num >>> 16);
        bytes[2] = (byte) (num >>> 8);
        bytes[3] = (byte) num;
        return bytes;
    }


    /**
     * 时标CP56Time2a解析
     * String aa = "50 C3 AD F1 13 05 12";
     * String[] s1 = aa.split(" ");
     * int[] t1 = new int[7];
     * for (int i = 0; i < s1.length; i++) {
     * t1[i] = Integer.parseInt(s1[i], 16);
     * }
     * <p>
     * System.out.println(TimeScale(t1));
     * <p>
     * b 时标CP56Time2a（长度为8 的int数组）
     *
     * @return 解析结果
     */
    public static String timeScale(String trim) {
        String[] s1 = trim.split(" ");
        String year = Integer.parseInt(s1[7], 16) + "";
        String month = Integer.parseInt(s1[6], 16) + "";
        if (month.length() ==1){
            month = "0"+month;
        }
        String day = Integer.parseInt(s1[5], 16) + "";
        if (day.length() ==1){
            day = "0"+day;
        }
        String hour = Integer.parseInt(s1[4], 16) + "";
        if (hour.length() ==1){
            hour = "0"+hour;
        }
        String minute = Integer.parseInt(s1[3], 16) + "";
        if (minute.length() ==1){
            minute = "0"+minute;
        }
        String second = Integer.parseInt(s1[2], 16) + "";
        if (second.length() ==1){
            second = "0"+second;
        }
        //str += "时标CP56Time2a:" + "20" + year + "-"
        //        + String.format("%02d", month) + "-"
        //        + String.format("%02d", day) + "," + hour + ":" + minute + ":"
        //        + second / 1000 + "." + second % 1000;
        //return str + "\n";
        return "20" + year + "-"
                + month + "-"
                + day + " " + hour + ":" + minute + ":"
                + second;
    }


    public static void main(String[] args) throws ParseException {
        //byte[] s = toBytes(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-06-19 17:43:12"));
        //for (int i = 0; i < s.length; i++) {
        //    System.out.println(Integer.toHexString(s[i]));
        //}


        String startTime = "00 00 08 08 11 12 01 15";
        //String startTime = "00 00 2B 11 13 06 12";
        //String endTime ="F0 D2 16 0F 0D 05 14";
        System.out.println(timeScale(startTime));
        //System.out.println(timeScale(endTime));

        //System.out.println(String.format("%04d","s"));
        //System.out.println(timeToCP56time2a("2018-06-19 17:43:12"));
        String s = CP56time2aUtil.timeToCP56time2a(new Date());
        System.out.println(s+"*****");

    }

    public static String timeToCP56time2a(Date date) {
        String tim = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        int yy = Integer.valueOf(tim.substring(2, 4));
        int MO = Integer.valueOf(tim.substring(4, 6));
        int DA = Integer.valueOf(tim.substring(6, 8));
        int HH = Integer.valueOf(tim.substring(8, 10));
        int MM = Integer.valueOf(tim.substring(10, 12));
        //高八位
        int S2 = (Integer.valueOf(tim.substring(12)) * 1000) >> 8;
        //低八位
        int S1 = (Integer.valueOf(tim.substring(12)) * 1000) & 0xFF;

        return getHexData(S1) + " " +
                getHexData(S2) + " " +
                getHexData(MM) + " " +
                getHexData(HH) + " " +
                getHexData(DA) + " " +
                getHexData(MO) + " " +
                getHexData(yy);

    }

    public static String getHexData(int num) {
        String s = Integer.toHexString(num);
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s.toUpperCase();
    }


}
